package com.shop.cloud.controller.hongfu;

import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.Shop;
import com.shop.cloud.domain.hongfu.ShopAndDistance;
import com.shop.cloud.domain.hongfu.StoreApplication;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.filter.IgnoreAuth;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.IStoreApplicationService;
import com.shop.cloud.service.hongfu.impl.ShopImageService;
import com.shop.cloud.service.hongfu.impl.ShopService;
import com.shop.cloud.service.hongfu.impl.UserService;
import com.shop.cloud.util.Page;

/**
 * 获取门店信息
 * 
 * @author xusai
 *
 */
@RestController
@RequestMapping(value = "api")
public class RoleController extends BaseController {
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ShopImageService shopImageService;
	
	@Autowired
	private IStoreApplicationService storeApplicationService;

	@IgnoreAuth
	@ApiOperation(value = "获取门店列表", notes = "获取门店列表")
	@RequestMapping(value = "/getShopLists", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ShopAndDistance> getShopList(@CurrentUserId Long userId,@RequestBody Shop shop) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageSize", shop.getPageSize_());
		params.put("pageIndex", shop.getPageIndex_());
		params.put("myLongitude", shop.getMyLongitude());
		params.put("myLatitude", shop.getMyLatitude());
		params.put("status", 2);
		
		
		
		//1：异业联盟2：公司网点
		if (shop.getIsUnion()!=null) {
			if (shop.getIsUnion()==1) {
				params.put("isUnion", Shop.UNION_DIFF);
			}
			if (shop.getIsUnion()==2) {
				params.put("isUnion", Shop.UNION_COMPANY);
			}
		}
		
		//1：距离排序
		if (shop.getIsDistance()!=null) {
			if (shop.getIsDistance()==1) {
				params.put("orderBy", "distance asc");
			}
			if (shop.getIsDistance()==2) {
				params.put("orderBy", "distance desc");
			}
		}
		
		//1根据好评率排序
		if (shop.getIsLevel()!=null) {
			if (shop.getIsLevel() == 1) {
				params.put("orderBy", "level desc");
			}
			if (shop.getIsLevel() == 2) {
				params.put("orderBy", "level asc");
			}
		}
		//全部:健康养生、餐饮美食...（后台分类）
		if (shop.getTypeId()!=null) {
			params.put("typeId", shop.getTypeId());
		}
		
		ShopAndDistance shopAndDistance = new ShopAndDistance();
		
		Page<Shop> list = shopService.findMap(params);
		
		
		shop.setLimitStart(0);
		shop.setLimitEnd(1);
		shop.setStatus(2);
		if(userId!=null){
			User tmpUser = userService.get(userId);
			if(tmpUser!=null&&tmpUser.getRoleId()!=null&&tmpUser.getRoleId().longValue()>-1){
		       shop.setRoleId(tmpUser.getRoleId());		
			}
		}
		
		Page<Shop> data = shopService.allPage(shop);
		if (shop.getIsExist()==null) {
			if (data.getData()!=null&&data.getData().size()>0) {
				for (int i = 0; i < list.getData().size(); i++) {
					if (list.getData().get(i).getId().equals(data.getData().get(0).getId())) {
						list.getData().remove(i);
					}
				}
			}
		}
		shopAndDistance.setDistanceList(list);
		shopAndDistance.setShopList(data);
		
//		for (int i = 0; i < list.size(); i++) {
//			data.add(list.get(i));
//		}

		return new ObjectDataResponse<ShopAndDistance>(shopAndDistance);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "添加门店加盟申请", notes = "添加门店加盟申请")
	@RequestMapping(value = "/addStore", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> addStore(@CurrentUserId Long userId, @RequestBody StoreApplication storeApplication){
		
		
		int flag = storeApplicationService.HandleAddStore(userId,storeApplication);
		
		return new ObjectDataResponse<Integer>(flag);
	}

}
